package com.engine.zhlx.financemoduel.pushdata.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.zhlx.financemoduel.pushdata.utils.PushDataUtil;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-09-10
 * @Description: Sap主动推送信息
 * @ModifyLog:
 **/

public class WeaPushDataWeb extends BaseBean {
    /**
     * @Description: SAP推送供应商主数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/pushVendorMainData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String pushVendorMainData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        JSONObject data = new JSONObject();
        String message = "推送成功";
        try {
            PushDataUtil pushDataUtil = new PushDataUtil();
            writeLog(">>>>>pushVendorMainData>>>paramsJson=" + paramsJson);
            String tablename = "uf_sapgyszsj";
            String checkParamsResult = pushDataUtil.checkPushVendorParams(paramsJson);
            int formmodeid = pushDataUtil.getFormmodeid(tablename);
            if ("SUCCESS".equals(checkParamsResult)) {
                JSONObject inputJson = JSONObject.parseObject(paramsJson.getString("INPUT"));
                String dataHeader = Util.null2String(inputJson.getString("DATAHEADER"));
                String dataItem = Util.null2String(inputJson.getString("DATAITEM"));
                String purItem = Util.null2String(inputJson.getString("PURITEM"));
                String fiItem = Util.null2String(inputJson.getString("FIITEM"));
                int dataHeaderCount = 0;
                int dataItemCount = 0;
                int purItemCount = 0;
                int fiItemCount = 0;
                JSONObject returnJson = JSONObject.parseObject(dataHeader);
                String gysbm = returnJson.getString("GYSBM");
                String gysmc1 = returnJson.getString("GYSMC1");
                String gysmc2 = returnJson.getString("GYSMC2");
                returnJson.put("gysqc", gysmc1 + gysmc2);
                int mainid = pushDataUtil.getMainId(tablename, "gysbm", gysbm);
                if (mainid <= 0) {
                    String formid = pushDataUtil.getFormid(tablename);
                    Map<String, Object> map = pushDataUtil.getMainField(returnJson,formid);
                    int dataid = pushDataUtil.insertModelData(formmodeid, map);
                    if (dataid > 0) {
                        JSONArray dataItemList = JSONArray.parseArray(dataItem);
                        if (!dataItemList.isEmpty()) {
                            for (int k = 0; k < dataItemList.size(); k++) {
                                JSONObject return1Json = dataItemList.getJSONObject(k);
                                String yxzhdm = return1Json.getString("YXZHDM");
                                return1Json.put("khyx", pushDataUtil.getKhyh(yxzhdm));
                                Map<String, Object> map1 = pushDataUtil.getDetailField(return1Json,formid,tablename + "_dt1");
                                map1.put("mainid", dataid);
                                boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt1", map1);
                                if (!insertStat) {
                                    dataItemCount++;
                                }
                            }
                        }
                        writeLog(">>>>>pushVendorMainData>>>dataItemListCount=" + dataItemList.size());
                        JSONArray purItemList = JSONArray.parseArray(purItem);
                        if (!purItemList.isEmpty()) {
                            for (int j = 0; j < purItemList.size(); j++) {
                                JSONObject return2Json = purItemList.getJSONObject(j);
                                Map<String, Object> map2 = pushDataUtil.getDetailField(return2Json,formid,tablename + "_dt2");
                                map2.put("mainid", dataid);
                                boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt2", map2);
                                if (!insertStat) {
                                    purItemCount++;
                                }
                            }
                        }
                        writeLog(">>>>>pushVendorMainData>>>purItemListCount=" + purItemList.size());
                        JSONArray fiItemList = JSONArray.parseArray(fiItem);
                        if (!fiItemList.isEmpty()) {
                            for (int i = 0; i < fiItemList.size(); i++) {
                                JSONObject returnJ3son = fiItemList.getJSONObject(i);
                                Map<String, Object> map3 = pushDataUtil.getDetailField(returnJ3son,formid,tablename + "_dt3");
                                map3.put("mainid", dataid);
                                boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt3", map3);
                                if (insertStat) {
                                    fiItemCount++;
                                }
                            }
                        }
                        writeLog(">>>>>pushVendorMainData>>>fiItemListCount=" + fiItemList.size());
                    } else {
                        dataHeaderCount++;
                    }

                } else {
                    String formid = pushDataUtil.getFormid(tablename);
                    Map<String, Object> map = pushDataUtil.getMainField(returnJson,formid);
                    boolean updateStat = pushDataUtil.updateModelData(tablename, mainid, map);
                    if (updateStat) {
                        JSONArray dataItemList = JSONArray.parseArray(dataItem);
                        if (!dataItemList.isEmpty()) {
                            pushDataUtil.deleteDetailData(tablename + "_dt1", mainid);
                            for (int k = 0; k < dataItemList.size(); k++) {
                                JSONObject return1Json = dataItemList.getJSONObject(k);
                                String yxzhdm = return1Json.getString("YXZHDM");
                                return1Json.put("khyx", pushDataUtil.getKhyh(yxzhdm));
                                Map<String, Object> map1 = pushDataUtil.getDetailField(return1Json,formid,tablename + "_dt1");
                                map1.put("mainid", mainid);
                                boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt1", map1);
                                if (insertStat) {
                                    dataItemCount++;
                                }
                            }
                        }
                        writeLog(">>>>>pushVendorMainData>>>dataItemListCount=" + dataItemList.size());
                        JSONArray purItemList = JSONArray.parseArray(purItem);
                        if (!purItemList.isEmpty()) {
                            pushDataUtil.deleteDetailData(tablename + "_dt2", mainid);
                            for (int j = 0; j < purItemList.size(); j++) {
                                JSONObject return2Json = purItemList.getJSONObject(j);
                                Map<String, Object> map2 = pushDataUtil.getDetailField(return2Json,formid,tablename + "_dt2");
                                map2.put("mainid", mainid);
                                boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt2", return2Json);
                                if (insertStat) {
                                    purItemCount++;
                                }
                            }
                        }
                        writeLog(">>>>>pushVendorMainData>>>purItemListCount=" + purItemList.size());
                        JSONArray fiItemList = JSONArray.parseArray(fiItem);
                        if (!fiItemList.isEmpty()) {
                            pushDataUtil.deleteDetailData(tablename + "_dt3", mainid);
                            for (int i = 0; i < fiItemList.size(); i++) {
                                JSONObject returnJ3son = fiItemList.getJSONObject(i);
                                Map<String, Object> map3 = pushDataUtil.getDetailField(returnJ3son,formid,tablename + "_dt3");
                                map3.put("mainid", mainid);
                                boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt3", returnJ3son);
                                if (insertStat) {
                                    fiItemCount++;
                                }
                            }
                        }
                        writeLog(">>>>>pushVendorMainData>>>fiItemListCount=" + fiItemList.size());
                    } else {
                        dataHeaderCount++;
                    }
                }
                int counts = 0;
                if (dataHeaderCount > 0) {
                    data.put("DATAHEADER", "参数推送失败(" + dataHeaderCount + ")，请检查参数字段");
                    counts++;
                }
                if (dataItemCount > 0) {
                    data.put("PURITEM", "参数推送失败(" + purItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (purItemCount > 0) {
                    data.put("DATAITEM", "参数推送失败(" + dataItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (fiItemCount > 0) {
                    data.put("FIITEM", "参数推送失败(" + fiItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (counts > 0) {
                    code = "PARAMETER_ERROR";
                    message = "推送失败，请检查参数字段";
                }

            } else {
                code = "PARAMETER_FORMAT_ERROR";
                message = checkParamsResult;
            }
        } catch (Exception e) {
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        apidatas.put("MSG_TYPE", "SUCCESS".equals(code)?"S":"E");
        apidatas.put("MSG_TEXT", message);
        writeLog(">>>>>pushVendorMainData>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: SAP推送物料主数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/pushMaterialMainData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String pushMaterialMainData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        JSONObject data = new JSONObject();
        String message = "推送成功";
        try {
            PushDataUtil pushDataUtil = new PushDataUtil();
            writeLog(">>>>>pushMaterialMainData>>>paramsJson=" + paramsJson);
            String tablename = "uf_wlzsj";
            int formmodeid = pushDataUtil.getFormmodeid(tablename);
            JSONObject inputJson = JSONObject.parseObject(paramsJson.getString("INPUT"));
            writeLog(">>>>>pushMaterialMainData>>>inputJson=" + inputJson);
            String dataStr = Util.null2String(inputJson.getString("DATA"));
            writeLog(">>>>>pushMaterialMainData>>>dataStr=" + dataStr);
            if (pushDataUtil.isJSONArray(dataStr)) {
                JSONArray dataList = JSONArray.parseArray(dataStr);
                int dataHeaderCount = 0;
                int ppItemCount = 0;
                int purItemCount = 0;
                int qmItemCount = 0;
                int saleItemCount = 0;
                int fiItemCount = 0;
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject dataItem = dataList.getJSONObject(i);
                    String dataHeader = Util.null2String(dataItem.getString("DATAHEADER"));
                    String ppItem = Util.null2String(dataItem.getString("PPITEM"));
                    String purItem = Util.null2String(dataItem.getString("PURITEM"));
                    String qmItem = Util.null2String(dataItem.getString("QMITEM"));
                    String saleItem = Util.null2String(dataItem.getString("SALEITEM"));
                    String fiItem = Util.null2String(dataItem.getString("FIITEM"));
                    JSONObject dataHeaderJson = JSONObject.parseObject(dataHeader);
                    String matnr = dataHeaderJson.getString("MATNR");
                    writeLog(">>>>>pushMaterialMainData>>>matnr=" + matnr);
                    int mainid = pushDataUtil.getMainId(tablename, "matnr", matnr);
                    writeLog(">>>>>pushMaterialMainData>>>mainid=" + mainid);
                    if (mainid <= 0) {
                        String formid = pushDataUtil.getFormid(tablename);
                        Map<String, Object> map = pushDataUtil.getMainField(dataHeaderJson,formid);
                        int dataid = pushDataUtil.insertModelData(formmodeid, map);
                        if (dataid > 0) {
                            if (pushDataUtil.isJSONArray(ppItem)) {
                                JSONArray ppItemList = JSONArray.parseArray(ppItem);
                                if (!ppItemList.isEmpty()) {
                                    for (int p = 0; p < ppItemList.size(); p++) {
                                        JSONObject returnJ1son = ppItemList.getJSONObject(p);
                                        Map<String, Object> map1 = pushDataUtil.getDetailField(returnJ1son,formid,tablename + "_dt1");
                                        map1.put("mainid", dataid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt1", map1);
                                        if (!insertStat) {
                                            ppItemCount++;
                                        }
                                    }
                                }
                                writeLog(">>>>>pushMaterialMainData>>>ppItemList=" + ppItemList.size());
                            }
                            if (pushDataUtil.isJSONArray(purItem)) {
                                JSONArray purItemList = JSONArray.parseArray(purItem);
                                if (!purItemList.isEmpty()) {
                                    for (int j = 0; j < purItemList.size(); j++) {
                                        JSONObject return2Json = purItemList.getJSONObject(j);
                                        Map<String, Object> map2 = pushDataUtil.getDetailField(return2Json,formid,tablename + "_dt2");
                                        map2.put("mainid", dataid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt2", map2);
                                        if (!insertStat) {
                                            purItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>purItemList=" + purItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(qmItem)) {
                                JSONArray qmItemList = JSONArray.parseArray(qmItem);
                                if (!qmItemList.isEmpty()) {
                                    for (int n = 0; n < qmItemList.size(); n++) {
                                        JSONObject returnJ3son = qmItemList.getJSONObject(n);
                                        Map<String, Object> map3 = pushDataUtil.getDetailField(returnJ3son,formid,tablename + "_dt3");
                                        map3.put("mainid", dataid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt3", map3);
                                        if (!insertStat) {
                                            qmItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>qmItemList=" + qmItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(saleItem)) {
                                JSONArray saleItemList = JSONArray.parseArray(saleItem);
                                if (!saleItemList.isEmpty()) {
                                    for (int m = 0; m < saleItemList.size(); m++) {
                                        JSONObject returnJ4son = saleItemList.getJSONObject(m);
                                        Map<String, Object> map4 = pushDataUtil.getDetailField(returnJ4son,formid,tablename + "_dt4");
                                        map4.put("mainid", dataid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt4", map4);
                                        if (!insertStat) {
                                            saleItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>saleItemList=" + saleItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(fiItem)) {
                                JSONArray fiItemList = JSONArray.parseArray(fiItem);
                                if (!fiItemList.isEmpty()) {
                                    for (int k = 0; k < fiItemList.size(); k++) {
                                        JSONObject return5Json = fiItemList.getJSONObject(k);
                                        Map<String, Object> map5 = pushDataUtil.getDetailField(return5Json,formid,tablename + "_dt5");
                                        map5.put("mainid", dataid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt5", map5);
                                        if (!insertStat) {
                                            fiItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>fiItemList=" + fiItemList.size());
                                }
                            }
                        } else {
                            dataHeaderCount++;
                        }
                    } else {
                        String formid = pushDataUtil.getFormid(tablename);
                        Map<String, Object> map = pushDataUtil.getMainField(dataHeaderJson,formid);
                        boolean updateStat = pushDataUtil.updateModelData(tablename, mainid, map);
                        if (updateStat) {
                            if (pushDataUtil.isJSONArray(ppItem)) {
                                JSONArray ppItemList = JSONArray.parseArray(ppItem);
                                if (!ppItemList.isEmpty()) {
                                    pushDataUtil.deleteDetailData(tablename + "_dt1", mainid);
                                    for (int p = 0; p < ppItemList.size(); p++) {
                                        JSONObject returnJ1son = ppItemList.getJSONObject(p);
                                        Map<String, Object> map1 = pushDataUtil.getDetailField(returnJ1son,formid,tablename + "_dt1");
                                        map1.put("mainid", mainid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt1", map1);
                                        if (!insertStat) {
                                            ppItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>ppItemList=" + ppItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(purItem)) {
                                JSONArray purItemList = JSONArray.parseArray(purItem);
                                if (!purItemList.isEmpty()) {
                                    pushDataUtil.deleteDetailData(tablename + "_dt2", mainid);
                                    for (int j = 0; j < purItemList.size(); j++) {
                                        JSONObject return2Json = purItemList.getJSONObject(j);
                                        Map<String, Object> map2 = pushDataUtil.getDetailField(return2Json,formid,tablename + "_dt2");
                                        map2.put("mainid", mainid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt2", map2);
                                        if (!insertStat) {
                                            purItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>purItemList=" + purItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(qmItem)) {
                                JSONArray qmItemList = JSONArray.parseArray(qmItem);
                                if (!qmItemList.isEmpty()) {
                                    pushDataUtil.deleteDetailData(tablename + "_dt3", mainid);
                                    for (int n = 0; n < qmItemList.size(); n++) {
                                        JSONObject returnJ3son = qmItemList.getJSONObject(n);
                                        Map<String, Object> map3 = pushDataUtil.getDetailField(returnJ3son,formid,tablename + "_dt3");
                                        map3.put("mainid", mainid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt3", map3);
                                        if (!insertStat) {
                                            qmItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>qmItemList=" + qmItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(saleItem)) {
                                JSONArray saleItemList = JSONArray.parseArray(saleItem);
                                if (!saleItemList.isEmpty()) {
                                    pushDataUtil.deleteDetailData(tablename + "_dt4", mainid);
                                    for (int m = 0; m < saleItemList.size(); m++) {
                                        JSONObject returnJ4son = saleItemList.getJSONObject(m);
                                        Map<String, Object> map4 = pushDataUtil.getDetailField(returnJ4son,formid,tablename + "_dt4");
                                        map4.put("mainid", mainid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt4", map4);
                                        if (!insertStat) {
                                            saleItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>saleItemList=" + saleItemList.size());
                                }
                            }
                            if (pushDataUtil.isJSONArray(fiItem)) {
                                JSONArray fiItemList = JSONArray.parseArray(fiItem);
                                if (!fiItemList.isEmpty()) {
                                    pushDataUtil.deleteDetailData(tablename + "_dt5", mainid);
                                    for (int k = 0; k < fiItemList.size(); k++) {
                                        JSONObject return5Json = fiItemList.getJSONObject(k);
                                        Map<String, Object> map5 = pushDataUtil.getDetailField(return5Json,formid,tablename + "_dt5");
                                        map5.put("mainid", mainid);
                                        boolean insertStat = pushDataUtil.insertDetailData(tablename + "_dt5", map5);
                                        if (!insertStat) {
                                            fiItemCount++;
                                        }
                                    }
                                    writeLog(">>>>>pushMaterialMainData>>>fiItemList=" + fiItemList.size());
                                }
                            }
                        } else {
                            dataHeaderCount++;
                        }
                    }
                }
                int counts = 0;
                if (dataHeaderCount > 0) {
                    data.put("DATAHEADER", "参数推送失败(" + dataHeaderCount + ")，请检查参数字段");
                    counts++;
                }
                if (ppItemCount > 0) {
                    data.put("PPITEM", "参数推送失败(" + ppItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (purItemCount > 0) {
                    data.put("PURITEM", "参数推送失败(" + purItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (qmItemCount > 0) {
                    data.put("QMITEM", "参数推送失败(" + qmItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (saleItemCount > 0) {
                    data.put("SALEITEM", "参数推送失败(" + saleItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (fiItemCount > 0) {
                    data.put("FIITEM", "参数推送失败(" + fiItemCount + ")，请检查参数字段");
                    counts++;
                }
                if (counts > 0) {
                    code = "PARAMETER_ERROR";
                    message = "推送失败，请检查参数字段";
                }
            } else {
                code = "PARAMETER_FORMAT_ERROR";
                message = "JSON格式化错误，请检查参数:DATA";
            }
        } catch (Exception e) {
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        apidatas.put("MSG_TYPE", "SUCCESS".equals(code)?"S":"E");
        apidatas.put("MSG_TEXT", message);
        writeLog(">>>>>pushMaterialMainData>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: Crm推送物料主数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */

}
